#!/usr/bin/env python
# -*- coding: UTF-8 -*-
'''
@Project ：group-learn 
@File    ：21. 合并两个有序链表.py
@IDE     ：PyCharm 
@Author  ：lcy-tester
@Date    ：2023/11/7 16:33 
'''
from typing import Optional


class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next


class Solution:
    def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
        cur = dum = ListNode(0)
        while list1 and list2:
            # 若当前 l1值 < l2值
            if list1.val < list2.val:
                # 当前指针等于 l1值   l1指针挪动到下一个值
                cur.next, list1 = list1, list1.next
            else:
                # 否则当前指针等于 l2值   l2指针挪动到下一个值
                cur.next, list2 = list2, list2.next
            # 当前指针挪动
            cur = cur.next
        # 如果某个列表为空，跳出，
        cur.next = list1 if list1 else list2
        return dum.next





if __name__ == '__main__':
    lis = [1, 2, 4]
    lis1 = [1, 3, 4]
    s = Solution()
    print(s.mergeTwoLists(lis, lis1))
